<?php
/**
 * Created by PhpStorm.
 * DateTime: 2020/10/30 11:47
 * Intro:
 */

namespace addons\podunk\controller;

use addons\podunk\model\User;
use think\Validate;

class Agent extends Base
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score','port','parent_id','pfull'];

    public function _initialize()
    {
        return parent::_initialize();
    }

    public function getList(){
        $search = $this->request->get('search');

        $this->success('代理列表', \addons\podunk\model\User::getAgentList($search));
    }

    /**
     * 注册会员
     *
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $mobile 手机号
     */
    public function create()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $nickname = $this->request->post('nickname');
        $email = $this->request->post('mobile') . '@' . request()->host();

        $mobile = $this->request->post('mobile');

        if (!$password) {
            $this->error(__('请填写密码')); //TODO:密码规则校验
        }
        if (strlen($password) < 6 || strlen($password) > 16) {
            $this->error(__('密码长度 6-16 位')); //TODO:密码规则校验
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号填写错误'));
        }
        $this->auth->setAllowFields($this->allowFields);
        $userInfo = $this->auth->getUserinfo();
        try{
            $ret =\addons\podunk\model\User::register($username, $password, $email, $mobile, [
                'parent_id'=>$this->auth->id,
                'pfull'=>$userInfo['pfull'].','.$this->auth->id,
                'nickname'=>$nickname]);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('注册成功');
    }

    public function rechargePort()
    {
        $params = $this->request->post();

        $this->podunkValidate($params, get_class(), 'rechargePort');

        $uid = $params['id'];
        $nickname = $params['nickname'];
        $port = $params['port'];

        $this->auth->setAllowFields($this->allowFields);

        $userInfo = $this->auth->getUserinfo();

        if($userInfo['port'] < $port){
            $this->error('端口不足，请联系管理员充值');
        }

        User::port('-'.$port, $this->auth->id, ' 充值减少 '.$nickname);

        User::port($port, $uid, ' 被充值增加 '.$userInfo['nickname']);


        $this->success('充值成功');
    }

    public function reducePort()
    {
        $params = $this->request->post();

        $this->podunkValidate($params, get_class(), 'reducePort');

        $uid = $params['id'];
        $nickname = $params['nickname'];
        $port = $params['port'];


        $agentPort = User::where('id',$uid)->value('port');

        $userInfo = $this->auth->getUserinfo();

        if($agentPort < $port){
            $this->error('代理可用端口不足，扣除端口失败');
        }

        User::port($port, $this->auth->id, ' 扣除增加 '.$nickname);

        User::port('-'.$port, $uid, ' 被扣除减少 '.$userInfo['nickname']);


        $this->success('扣除成功');
    }
}
